//数组转树
let data = [
    { id: 0, parentId: null, name: "生物" },
    { id: 1, parentId: 0, name: "动物" },
    { id: 2, parentId: 0, name: "植物" },
    { id: 3, parentId: 0, name: "微生物" },
    { id: 4, parentId: 1, name: "哺乳动物" },
    { id: 5, parentId: 1, name: "卵生动物" },
    { id: 6, parentId: 2, name: "种子植物" },
    { id: 7, parentId: 2, name: "蕨类植物" },
    { id: 8, parentId: 4, name: "大象" },
    { id: 9, parentId: 4, name: "海豚" },
    { id: 10, parentId: 4, name: "猩猩" },
    { id: 11, parentId: 5, name: "蟒蛇" },
    { id: 12, parentId: 5, name: "麻雀" },
];

function transTree(data) {
    if (!Array.isArray(data)) return;
    let result = [],
        map = {};
    data.forEach((item) => {
        map[item.id] = item;
    });
    data.forEach((item) => {
        let parent = map[item.parentId];
        if (parent) {
            (parent.children || (parent.children = [])).push(item);
        } else {
            result.push(item);
        }
    });
    return result;
}

console.log(JSON.stringify(transTree(data)));

//树转数组
let node = [
    {
        id: 0,
        parentId: null,
        name: "生物",
        children: [
            {
                id: 1,
                parentId: 0,
                name: "动物",
                children: [
                    {
                        id: 4,
                        parentId: 1,
                        name: "哺乳动物",
                        children: [
                            {
                                id: 8,
                                parentId: 4,
                                name: "大象",
                            },
                            {
                                id: 9,
                                parentId: 4,
                                name: "海豚",
                            },
                            {
                                id: 10,
                                parentId: 4,
                                name: "猩猩",
                            },
                        ],
                    },
                    {
                        id: 5,
                        parentId: 1,
                        name: "卵生动物",
                        children: [
                            {
                                id: 11,
                                parentId: 5,
                                name: "蟒蛇",
                            },
                            {
                                id: 12,
                                parentId: 5,
                                name: "麻雀",
                            },
                        ],
                    },
                ],
            },
            {
                id: 2,
                parentId: 0,
                name: "植物",
                children: [
                    {
                        id: 6,
                        parentId: 2,
                        name: "种子植物",
                    },
                    {
                        id: 7,
                        parentId: 2,
                        name: "蕨类植物",
                    },
                ],
            },
            {
                id: 3,
                parentId: 0,
                name: "微生物",
            },
        ],
    },
];

function transArr(node) {
    let queue = node;
    let result = [];
    while (queue.length > 0) {
        let cur = queue.shift();
        let { children, ...item } = cur;
        result.push(item);
        if (cur.children) {
            cur.children.forEach((item) => {
                queue.push(item);
            });
        }
    }
    return result;
}

// let res = transArr(node)
// console.log(res)
